let app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    images: [],
    title: '',
    show_time: '请选择日期',
    content: '',
    condition_val: [
      {
        value: 1,
        name: '运行正常企业'
      },
      {
        value: 2,
        name: '困难企业'
      },
      {
        value: 4,
        name: '停产企业'
      }
    ],
    condition_key: -1,
    problemType: [],
    problemKey: -1,
    visitor: '',
    endTime: 0,
    startTime: 0,
    solutionType: [],
    solutionKey: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var date = this.formatTime(new Date());
    // 再通过setData更改Page()里面的data，动态更新页面的数据
    this.setData({
      show_time: date
    });
    this.setData({ projectId: options.id });
    this.problemTypeApi();
  },


  formatTime: function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()

    //设置最大日期
    var endday = date.getDate() - 5;
    this.setData({ endTime: year + "-" + month + "-" + day });
    this.setData({ startTime: year + "-" + month + "-" + endday });

    return year + "-" + month + "-" + day;
  },

  //   获取问题类型
  problemTypeApi: function () {

    let params = {
      id: this.data.projectId,
      title: this.data.title,
      show_time: this.data.show_time,
      content: this.data.content,
      images: this.data.images.join(';')
    };
    // console.log(params)
    app.request('post', '/api/project/problemTypeApi', params).then(res => {
      this.setData({
        problemType: res.data.data,
        solutionType: res.data.solution
      });
    }).catch(err => console.log(err));

  },

  /** 输入标题 */
  inputTitle(e) {
    this.setData({ title: e.detail.value });
  },

  /** 选择时间 */
  changeDate(e) {
    this.setData({ show_time: e.detail.value });
  },

  /** 输入内容 */
  inputArea(e) {
    var fieldKey = e.currentTarget.dataset.field;
    var value = e.detail.value;
    if (fieldKey == 'visitor') {
      this.setData({ visitor: value });
    }
    if (fieldKey == 'content') {
      this.setData({ content: value });
    }
    if (fieldKey == 'problem_content') {
      this.setData({ problem_content: value });
    }
  },

  // 企业状况选择
  conditionClick: function (e) {
    this.setData({ condition_key: e.detail.value });
  },

  //问题类型选择
  problemClick: function (e) {
    this.setData({ problemKey: e.detail.value });
  },

  //诉求解决情况选择
  solutionClick: function (e) {
    this.setData({ solutionKey: e.detail.value });
  },

  onChoosePic() {
    let that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: `${app.globalData.rootUrl}/api/upload/upload`,
          filePath: tempFilePaths[0],
          name: 'file',
          formData: { 'appid': app.appid },
          success: res => {
            let response = JSON.parse(res.data);
            if (res.statusCode == 200) {
              if (response.code == 0) {
                let images = that.data.images;
                images.push(response.thumb);
                that.setData({ images });
              } else {
                app.showFailToast(response.msg);
              }
            } else app.showFailToast('网络错误，请稍后再试');
          }
        });
      }
    });
  },

  /** 取消 */
  onCancel() {
    wx.navigateBack();
  },

  onSumit() {
    // if(this.data.title == ''){
    //     app.showFailToast('请填写标题');
    //     return false;
    // }
    var visitor = this.data.visitor; //走访人
    var show_time = this.data.show_time; //走访时间
    var content = this.data.content;  //走访情况
    var condition_key = this.data.condition_key;  //企业状况  
    var problemKey = this.data.problemKey;  //问题类型
    var problem_content = this.data.problem_content;  //问题内容
    var solutionKey = this.data.solutionKey;  //诉求解决情况
    if (visitor == '') {
      app.showFailToast('请填写走访人');
      return false;
    }
    if (show_time == '请选择日期') {
      app.showFailToast('请选择走访时间');
      return false;
    }
    if (content == '') {
      app.showFailToast('请填写走访内容');
      return false;
    }
    if (condition_key == -1) {
      app.showFailToast('请选择企业状况');
      return false;
    }
    // if (this.data.images.length == 0) {
    //   app.showFailToast('请上传图片');
    //   return false;
    // }

    var condition_val = this.data.condition_val;
    var problemType = this.data.problemType;

    let params = {
      id: this.data.projectId,
      visitor: visitor,
      show_time: show_time,
      content: content,
      condition_val: condition_val[condition_key].value,
      problem_id: problemType[problemKey].id,
      problem_content: problem_content,
      type: 1,
      images: this.data.images.join(';'),
      solution:solutionKey
    };
    // console.log(params)
    app.request('post', '/api/project/figure', params).then(res => {
      app.showSuccessToast('提交成功');
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
    }).catch(err => console.log(err));
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})